package infb.servlet.movil;

import infb.dao.MedicosInterfaz;
import infb.dao.PacientesInterfaz;
import infb.dao.TelecitasInterfaz;

import infb.modelo.Medicos;
import infb.modelo.Pacientes;
import infb.modelo.Telecitas;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Audio extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NamingException {
       
        Connection con = null;


        DataOutputStream dataOut = new DataOutputStream(response.getOutputStream());
        DataInputStream dis = new DataInputStream(request.getInputStream());
     
        Context contexto = new InitialContext();
        TelecitasInterfaz telecitas = (TelecitasInterfaz) contexto.lookup("TelecitasEJB");

        MedicosInterfaz medicos = (MedicosInterfaz) contexto.lookup("MedicosEJB");
        PacientesInterfaz pacientes = (PacientesInterfaz) contexto.lookup("PacientesEJB");

     
        Medicos medico = medicos.find("74655258L");
       
        
         
        byte[] audio;
        int size;

        int tamano = dis.readInt();
        audio = new byte[tamano];

        dis.readFully(audio, 0, tamano);
        String dni=dis.readUTF();
        Pacientes paciente= pacientes.buscarPorDNI(dni);
        //Long id=paciente.getId();
   
        OutputStream os = new FileOutputStream("C:/workspace/InfB/GestionHospitalaria/web/audio/audio"+paciente.getId()+medico.getDni()+".wav");
        Telecitas telecita = new Telecitas();

        telecita.setMedico(medico);
        telecita.setSonido(audio);
        telecita.setPaciente(paciente);
        telecita.setFecha(new Date());

        telecitas.create(telecita);

        os.write(audio, 0, audio.length);
        os.close();
        dis.close();

        //System.out.println("telecita.getSonido()"+telecita.getSonido());
       /* OutputStream os2 = new FileOutputStream("C:/audio2.wma");

        os2.write(telecita.getSonido(), 0, telecita.getSonido().length);
        os2.close();*/

    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(Audio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(Audio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
